package com.bloom.opensourceframework.okhttp.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;

import com.bloom.opensourceframework.R;
import com.bloom.opensourceframework.activity.BaseActivity;
import com.bloom.opensourceframework.eventbus.entity.MessageEvent;
import com.bloom.opensourceframework.okhttp.presenter.OkHttpPresenter;
import com.bloom.opensourceframework.okhttp.presenter.contact.OkHttpContact;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by bloom on 2018/5/3.
 */

public class OkHttpExampleActivity extends BaseActivity implements OkHttpContact.IView {

    private final String TAG = this.getClass().getSimpleName();

    private OkHttpPresenter mOkHttpPresenter;
    private TextView tvContent;
    private TextView tvAsynContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        setTitle("OKHttp");
        initView();
        initData();
    }

    private void initView() {
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvAsynContent = (TextView) findViewById(R.id.tv_content2);
        mOkHttpPresenter = new OkHttpPresenter(this);
    }

    private void initData() {
        mOkHttpPresenter.getRequstAsyn();
    }

    @Override
    public void updateView() {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void synUpdate(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvContent.setText("syn:" + str);
            }
        });
    }

    @Override
    public void asynUpdate(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvAsynContent.setText("aysn:" + str);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EventBus.getDefault().post(new MessageEvent(TAG + ": back finish"));
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
